package com.example.a260219.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.net.DatagramPacket;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import utils.UDPSocket;

/**
 * Created by 260219 on 2018/4/10.
 */

public class AndroidUdpNewAcitivity extends AppCompatActivity {

    @InjectView(R.id.tv_receive_ip)
    EditText tvReceiveIp;
    @InjectView(R.id.tv_receive_port)
    TextView tvReceivePort;
    @InjectView(R.id.btn_start)
    Button btnStart;
    @InjectView(R.id.tv_service_msg)
    EditText tvServiceMsg;
    @InjectView(R.id.btn_send)
    Button btnSend;
    @InjectView(R.id.btn_reset)
    Button btnReset;
    @InjectView(R.id.tv_received_ip)
    TextView tvReceivedIp;
    @InjectView(R.id.tv_receive_msg)
    TextView tvReceiveMsg;

    private UDPSocket socket;
    private String rip;//接收端ip
    private String sip;//服务端ip
    private int port;
    private String message;
    private DatagramPacket receivePacket;
    int count = 0;
    public Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            receivePacket = (DatagramPacket) msg.obj;
            String strReceive = new String(receivePacket.getData(), 0, receivePacket.getLength());
            tvReceivedIp.setText(receivePacket.getAddress().getHostAddress());
            tvReceivePort.setText(receivePacket.getPort() + "");
            tvReceiveMsg.setText(strReceive);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_udp);
        ButterKnife.inject(this);
    }

    public void myToast(String str) {
        Toast.makeText(AndroidUdpNewAcitivity.this, str, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.btn_start, R.id.btn_send,R.id.btn_reset})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                if(TextUtils.isEmpty(tvReceiveIp.getText())||TextUtils.isEmpty(tvReceivePort.getText())){
                    myToast("接收端ip或者port不能为空");
                    return;
                }

                rip=tvReceiveIp.getText().toString();
                port=Integer.parseInt(tvReceivePort.getText().toString());

                if(socket==null){
                    socket = new UDPSocket(rip, port, this);
                }
                //注册回调函数
                socket.setUIUpdateListener(new UDPSocket.UIUpdateListener() {
                    @Override
                    public void upUI(DatagramPacket receivePacket) {
                        Message msg = new Message();
                        msg.obj = receivePacket;
                        mhandler.sendMessage(msg);
                    }
                });
                break;
            case R.id.btn_send:
                if(TextUtils.isEmpty(tvServiceMsg.getText().toString())){
                    myToast("发送消息不能为空");
                    return;
                }
                message=tvServiceMsg.getText().toString();
                ++count;//记录点击发送的次数
                socket.sendMessage(message+count);
                break;
            case R.id.btn_reset:
                tvReceivedIp.setText("");
                tvReceiveMsg.setText("");
                break;
        }
    }


}
